﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductIN_M_Add(ProductIN_M model, int ACID);

        [OperationContract]
        string ProductIN_M_Update(ProductIN_M model, int ACID);

        [OperationContract]
        string ProductIN_MBydid_Delete(string billid, int ACID);

        [OperationContract]
        ProductIN_M ProductIN_MBydid_Get(string billid, int ACID);

        [OperationContract]
        List<ProductIN_M> ProductIN_M_Get(string sortBy, int ACID);
    }
    public partial class Service : IService
    {

        public string ProductIN_M_Add(ProductIN_M model, int ACID)
        {
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }
            if (model.total == null)
            {
                return "『总金额』不能为空！";
            }
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.billdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_M.Count(p => p.billid == model.billid) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductIN_M.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }

        #region  添加ProductIN_M表数据事物用重构
        /// <summary>
        /// 添加ProductIN_M事物用重构
        /// </summary>
        /// <param name="model">ProductIN_M实体</param>
        /// <returns>空或错误</returns>
        private string ProductIN_M_Add(ProductIN_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }
            if (model.ischeck == null)
            {
                return "『复核标志』不能为空！";
            }
            if (model.balance == null)
            {
                return "『余额』不能为空！";
            }

            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.billdate == null)
            {
                return "『开单日期』不能为空！";
            }
            if (ctx.ProductIN_M.Count(p => p.billid == model.billid) > 0)
            {
                return "主键已存在！";
            }
            ctx.ProductIN_M.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion

        public string ProductIN_M_Update(ProductIN_M model, int ACID)
        {
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }
            if (model.total == null)
            {
                return "『总金额』不能为空！";
            }
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.billdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_M.Count(b => b.billid == model.billid) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    ProductIN_M proIn_m = ctx.ProductIN_M.Single(b => b.billid == model.billid);
                    if (!string.IsNullOrEmpty(model.billid))
                    {
                        proIn_m.billid = model.billid;
                    }

                    if (model.opID != null || model.opID.Equals(Guid.Empty))
                    {
                        proIn_m.opID = model.opID;
                    }
                    if (model.handleID != null || model.handleID.Equals(Guid.Empty))
                    {
                        proIn_m.handleID = model.handleID;
                    }
                    if (model.auditor != null || model.auditor.Equals(Guid.Empty))
                    {
                        proIn_m.auditor = model.auditor;
                    }
                    if (model.clientID != null || model.clientID.Equals(Guid.Empty))
                    {
                        proIn_m.clientID = model.clientID;
                    }
                    proIn_m.cash = model.cash;
                    if (model.billdate != null)
                    {
                        proIn_m.billdate = model.billdate;
                    }
                    proIn_m.containTax = model.containTax;
                    if (proIn_m.paydate != null)
                    {
                        proIn_m.paydate = model.paydate;
                    }
                    proIn_m.account = model.account;
                    proIn_m.voucher = model.voucher;
                    proIn_m.cashier = model.cashier;
                    proIn_m.distotal = model.distotal;
                    proIn_m.total = model.total;
                    proIn_m.balance = model.balance;
                    proIn_m.ischeck = model.ischeck;
                    proIn_m.billnoTypes = model.billnoTypes;
                    proIn_m.Remarks = model.Remarks;

                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }

        #region  添加ProductIN_M表数据事物用重构
        /// <summary>
        /// 添加ProductIN_M事物用重构
        /// </summary>
        /// <param name="model">ProductIN_M实体</param>
        /// <param name="ACID"></param>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public string ProductIN_M_Update(ProductIN_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }
            if (model.total <0)
            {
                return "『总金额』不能为空！";
            }
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.billdate == null)
            {
                return "『开单日期』不能为空！";
            }

            try
            {
                if (ctx.ProductIN_M.Count(b => b.billid == model.billid) != 1)
                {
                    return "未找到指定记录或记录不唯一！";
                }
                ProductIN_M proIn_m = ctx.ProductIN_M.Single(b => b.billid == model.billid);
                if (!string.IsNullOrEmpty(model.billid))
                {
                    proIn_m.billid = model.billid;
                }

                if (model.opID != null || model.opID.Equals(Guid.Empty))
                {
                    proIn_m.opID = model.opID;
                }
                if (model.handleID != null || model.handleID.Equals(Guid.Empty))
                {
                    proIn_m.handleID = model.handleID;
                }
                if (model.auditor != null || model.auditor.Equals(Guid.Empty))
                {
                    proIn_m.auditor = model.auditor;
                }
                if (model.clientID != null || model.clientID.Equals(Guid.Empty))
                {
                    proIn_m.clientID = model.clientID;
                }
                proIn_m.cash = model.cash;
                if (model.billdate != null)
                {
                    proIn_m.billdate = model.billdate;
                }
                proIn_m.containTax = model.containTax;
                if (proIn_m.paydate != null)
                {
                    proIn_m.paydate = model.paydate;
                }
                proIn_m.account = model.account;
                proIn_m.voucher = model.voucher;
                proIn_m.cashier = model.cashier;
                proIn_m.distotal = model.distotal;
                proIn_m.total = model.total;
                proIn_m.balance = model.balance;
                proIn_m.ischeck = model.ischeck;
                proIn_m.billnoTypes = model.billnoTypes;
                proIn_m.Remarks = model.Remarks;

                decimal total = model.total - proIn_m.total;
                RecordBalance(total, proIn_m.clientID.Value, ctx, ACID);
                //ctx.SubmitChanges();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return " 修改失败!错误详细信息：" + ex.Message;
            }
        }
        #endregion

        public string ProductIN_MBydid_Delete(string billid, int ACID)
        {
            throw new NotImplementedException();
        }

        public ProductIN_M ProductIN_MBydid_Get(string billid, int ACID)
        {
            if (string.IsNullOrEmpty(billid))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_M.Count(b => b.billid == billid) != 1)
                    {
                        return null;
                    }
                    return ctx.ProductIN_M.SingleOrDefault(b => b.billid == billid);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }

        public List<ProductIN_M> ProductIN_M_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return ProductIN_M_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

        #region  获取ProductIN_M表数据
        /// 获取ProductIN_M
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<ProductIN_M> ProductIN_M_Get(Expression<Func<ProductIN_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    List<ProductIN_M> list = new List<ProductIN_M>();
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            list= ctx.ProductIN_M.ToList();
                        }
                        else
                        {
                            list= ctx.ProductIN_M.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            list= ctx.ProductIN_M.Where(predicate).ToList();
                        }
                        else
                        {
                            list= ctx.ProductIN_M.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }

                    return list;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion
    }
}
